<template>
	<view>
		<navbar title="蜂竞技" />
		<view class="label-group">
			<text class="label">初始化与监听</text>
		</view>
		<view class="hr"></view>
		<view class="function">
			<view class="button" @click="init">
				<text class="label">初始化</text>
			</view>
			<view class="button" @click="setSentenceRecognizerListener">
				<text class="label">设置监听</text>
			</view>
			<view class="button" @click="removeSentenceRecognizerListener">
				<text class="label">移除监听</text>
			</view>
			<view class="button" @click="destroy">
				<text class="label">销毁</text>
			</view>
		</view>
		<view class="label-group">
			<text class="label">一句话识别</text>
		</view>
		<view class="hr"></view>
		<view class="function">
			<view class="button" @click="recognize">
				<text class="label">语音识别</text>
			</view>
			<view class="button" @click="testing">
				<text class="label">testing</text>
			</view>
		</view>
		<view class="label-group">
			<text class="label">辅助函数</text>
		</view>
		<view class="hr"></view>
		<view class="function">
			<view class="button" @click="startRecord">
				<text class="label">开始录音</text>
			</view>
			<view class="button" @click="stopRecord">
				<text class="label">结束录音</text>
			</view>
			<view class="button" @click="playRecord">
				<text class="label">播放录音</text>
			</view>
		</view>
	</view>
</template>

<script>
	var recognizer = uni.requireNativePlugin("RY-TencentSentenceRecognizer");

	const recorderManager = uni.getRecorderManager();
	const innerAudioContext = uni.createInnerAudioContext();
	innerAudioContext.autoplay = true;
	import navbar from '../../../components/gamesNav.vue'
	export default {
		data() {
			return {
				secretKey: 'V2X3d8ulhirvtkVFtPygTJa0Nl6bCHVe',
				secretId: 'AKIDDYp3Nf0FR4vpeNzW9gK1oYSMCHONMKNB',
				appId: '1323065199',
				filePath: '',
			}
		},
		components: {
			navbar,
		},
		onLoad() {
			let that = this;
			recorderManager.onStop(function(res) {
				that.filePath = plus.io.convertLocalFileSystemURL(res.tempFilePath);
				uni.showToast({
					title: '录音文件为: ' + JSON.stringify(that.filePath),
					position: "bottom"
				});
			});
		},
		methods: {
			testing() {
				var that = this;
				var content = 'pardon';
				var result = that.$util.fnCheckResultOnlyTips(content);

				uni.showModal({
					content: content + '：' + result,
					showCancel: false
				});
			},


			init() {
				var that = this;
				recognizer.init({
					appId: that.appId,
					secretId: that.secretId,
					secretKey: that.secretKey
				}, ret => {
					uni.showModal({
						content: JSON.stringify(ret),
						showCancel: false
					});
				});
			},
			setSentenceRecognizerListener() {
				var that = this;
				recognizer.setSentenceRecognizerListener({}, ret => {
					if (ret.hasOwnProperty('eventType')) {
						// uni.showToast({
						// 	title: JSON.stringify(ret),
						// 	position: "top"
						// });

						if (ret.eventType == "onRecognize") {
							var content = ret.text;
							var result = that.$util.fnCheckResultOnlyTips(content);

							uni.showModal({
								content: content + '：' + result,
								showCancel: false
							});
						}
					} else {
						uni.showModal({
							content: JSON.stringify(ret),
							showCancel: false
						});
					}
				});
			},
			removeSentenceRecognizerListener() {
				recognizer.removeSentenceRecognizerListener({}, ret => {
					uni.showModal({
						content: JSON.stringify(ret),
						showCancel: false
					});
				});
			},
			destroy() {
				recognizer.destroy({}, ret => {
					uni.showModal({
						content: JSON.stringify(ret),
						showCancel: false
					});
				});
			},

			recognize() {
				let that = this;

				if (that.filePath == '') {
					uni.showToast({
						title: '录音文件不存在，请先使用辅助函数开始录音1',
						position: "bottom"
					});
					return;
				}

				recognizer.recognize({
					filePath: that.filePath
				}, ret => {
					uni.showModal({
						content: JSON.stringify(ret),
						showCancel: false
					});
				});
			},
			startRecord() {
				recorderManager.start();

				uni.showToast({
					title: '开始录音',
					position: "bottom"
				});
			},
			stopRecord() {
				recorderManager.stop();

				uni.showToast({
					title: '结束录音',
					position: "bottom"
				});
			},
			playRecord() {
				let that = this;
				if (that.filePath == '') {
					uni.showToast({
						title: '没有录音可以播放，请录音',
						position: "bottom"
					});
					return;
				}

				innerAudioContext.src = this.filePath;
				innerAudioContext.play();

				uni.showToast({
					title: '播放录音',
					position: "bottom"
				});
			}
		}
	}
</script>

<style>
	.trtc {
		position: fixed;
		top: 0;
		left: 0;
		background-color: #EBEBEB;
	}

	.content {
		background-color: #F7F8FA;
	}

	.label-group {
		margin-top: 15rpx;
		background-color: transparent;
		height: 65rpx;
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		justify-content: center;
		flex-direction: column;
		padding-left: 20rpx;
	}

	.label {
		font-size: 30rpx;
		color: #333333;
	}

	.function {
		margin: 10rpx 20rpx 0;
	}

	.button {
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		justify-content: center;
		align-items: center;
		flex-direction: row;
		margin-top: 12rpx;
		height: 74rpx;
		background-color: #fff;
		border: 1rpx solid #EBEBEB;
		border-radius: 12rpx;
	}

	.button:active {
		background-color: #F1F1F1;
		opacity: 0.5;
	}

	.company-group {
		position: fixed;
		bottom: 35rpx;
		width: 100%;
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		justify-content: center;
		align-items: center;
		flex-direction: row;
		background-color: transparent;
	}

	.company {
		color: rgb(153, 150, 150);
		font-size: 28rpx;
	}
</style>